// 简单的LSP配置测试，不依赖其他有问题的crate

#[test]
fn test_basic_functionality() {
    // 基本的测试，确保代码可以编译
    assert_eq!(2 + 2, 4);
}

#[cfg(test)]
mod config_tests {
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    // 复制必要的结构体定义，避免依赖其他crate
    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestLspServerConfig {
        pub name: String,
        pub command: String,
        pub args: Vec<String>,
        pub file_extensions: Vec<String>,
        pub language_ids: Vec<String>,
        pub enabled: bool,
        pub auto_restart: bool,
        pub max_restart_count: u32,
        pub restart_interval: u64,
        pub health_check_interval: u64,
    }

    impl Default for TestLspServerConfig {
        fn default() -> Self {
            Self {
                name: String::new(),
                command: String::new(),
                args: Vec::new(),
                file_extensions: Vec::new(),
                language_ids: Vec::new(),
                enabled: true,
                auto_restart: true,
                max_restart_count: 3,
                restart_interval: 5,
                health_check_interval: 30,
            }
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestGlobalLspSettings {
        pub enable_diagnostics: bool,
        pub enable_completion: bool,
        pub max_completion_items: usize,
        pub diagnostic_delay_ms: u64,
        pub max_concurrent_servers: usize,
        pub server_startup_timeout: u64,
        pub request_timeout_ms: u64,
    }

    impl Default for TestGlobalLspSettings {
        fn default() -> Self {
            Self {
                enable_diagnostics: true,
                enable_completion: true,
                max_completion_items: 100,
                diagnostic_delay_ms: 500,
                max_concurrent_servers: 10,
                server_startup_timeout: 30,
                request_timeout_ms: 5000,
            }
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestLspManagerConfig {
        pub servers: HashMap<String, TestLspServerConfig>,
        pub global_settings: TestGlobalLspSettings,
    }

    impl Default for TestLspManagerConfig {
        fn default() -> Self {
            let mut servers = HashMap::new();

            // Python服务器配置
            servers.insert("python".to_string(), TestLspServerConfig {
                name: "Python Language Server".to_string(),
                command: "pylsp".to_string(),
                args: vec![],
                file_extensions: vec![".py".to_string(), ".pyi".to_string()],
                language_ids: vec!["python".to_string()],
                ..Default::default()
            });

            // TypeScript服务器配置
            servers.insert("typescript".to_string(), TestLspServerConfig {
                name: "TypeScript Language Server".to_string(),
                command: "typescript-language-server".to_string(),
                args: vec!["--stdio".to_string()],
                file_extensions: vec![".ts".to_string(), ".tsx".to_string(), ".js".to_string(), ".jsx".to_string()],
                language_ids: vec!["typescript".to_string(), "javascript".to_string()],
                ..Default::default()
            });

            Self {
                servers,
                global_settings: TestGlobalLspSettings::default(),
            }
        }
    }

    impl TestLspManagerConfig {
        fn get_server_for_extension(&self, extension: &str) -> Option<&TestLspServerConfig> {
            self.servers.values().find(|config| {
                config.enabled && config.file_extensions.contains(&extension.to_string())
            })
        }

        fn get_server_for_language(&self, language_id: &str) -> Option<&TestLspServerConfig> {
            self.servers.values().find(|config| {
                config.enabled && config.language_ids.contains(&language_id.to_string())
            })
        }
    }

    #[test]
    fn test_default_config_creation() {
        let config = TestLspManagerConfig::default();
        
        // 检查全局设置
        assert!(config.global_settings.enable_diagnostics);
        assert!(config.global_settings.enable_completion);
        assert_eq!(config.global_settings.max_completion_items, 100);
        assert_eq!(config.global_settings.diagnostic_delay_ms, 500);
        
        // 检查默认服务器配置
        assert!(config.servers.contains_key("python"));
        assert!(config.servers.contains_key("typescript"));
    }

    #[test]
    fn test_server_config_defaults() {
        let config = TestLspServerConfig::default();
        
        assert!(config.enabled);
        assert!(config.auto_restart);
        assert_eq!(config.max_restart_count, 3);
        assert_eq!(config.restart_interval, 5);
        assert_eq!(config.health_check_interval, 30);
        assert!(config.file_extensions.is_empty());
        assert!(config.language_ids.is_empty());
    }

    #[test]
    fn test_python_server_config() {
        let config = TestLspManagerConfig::default();
        let python_config = config.servers.get("python").unwrap();
        
        assert_eq!(python_config.name, "Python Language Server");
        assert_eq!(python_config.command, "pylsp");
        assert!(python_config.args.is_empty());
        assert!(python_config.file_extensions.contains(&".py".to_string()));
        assert!(python_config.file_extensions.contains(&".pyi".to_string()));
        assert!(python_config.language_ids.contains(&"python".to_string()));
        assert!(python_config.enabled);
    }

    #[test]
    fn test_typescript_server_config() {
        let config = TestLspManagerConfig::default();
        let ts_config = config.servers.get("typescript").unwrap();
        
        assert_eq!(ts_config.name, "TypeScript Language Server");
        assert_eq!(ts_config.command, "typescript-language-server");
        assert_eq!(ts_config.args, vec!["--stdio".to_string()]);
        assert!(ts_config.file_extensions.contains(&".ts".to_string()));
        assert!(ts_config.file_extensions.contains(&".tsx".to_string()));
        assert!(ts_config.file_extensions.contains(&".js".to_string()));
        assert!(ts_config.file_extensions.contains(&".jsx".to_string()));
        assert!(ts_config.language_ids.contains(&"typescript".to_string()));
        assert!(ts_config.language_ids.contains(&"javascript".to_string()));
    }

    #[test]
    fn test_get_server_for_extension() {
        let config = TestLspManagerConfig::default();
        
        // 测试Python文件
        let python_server = config.get_server_for_extension(".py");
        assert!(python_server.is_some());
        assert_eq!(python_server.unwrap().command, "pylsp");
        
        // 测试TypeScript文件
        let ts_server = config.get_server_for_extension(".ts");
        assert!(ts_server.is_some());
        assert_eq!(ts_server.unwrap().command, "typescript-language-server");
        
        // 测试未知扩展名
        let unknown_server = config.get_server_for_extension(".unknown");
        assert!(unknown_server.is_none());
    }

    #[test]
    fn test_get_server_for_language() {
        let config = TestLspManagerConfig::default();
        
        // 测试Python语言
        let python_server = config.get_server_for_language("python");
        assert!(python_server.is_some());
        assert_eq!(python_server.unwrap().command, "pylsp");
        
        // 测试TypeScript语言
        let ts_server = config.get_server_for_language("typescript");
        assert!(ts_server.is_some());
        assert_eq!(ts_server.unwrap().command, "typescript-language-server");
        
        // 测试JavaScript语言（应该返回TypeScript服务器）
        let js_server = config.get_server_for_language("javascript");
        assert!(js_server.is_some());
        assert_eq!(js_server.unwrap().command, "typescript-language-server");
        
        // 测试未知语言
        let unknown_server = config.get_server_for_language("unknown");
        assert!(unknown_server.is_none());
    }

    #[test]
    fn test_config_serialization() {
        let config = TestLspManagerConfig::default();
        
        // 测试序列化
        let json_str = serde_json::to_string_pretty(&config).unwrap();
        assert!(!json_str.is_empty());
        assert!(json_str.contains("servers"));
        assert!(json_str.contains("global_settings"));
        assert!(json_str.contains("python"));
        assert!(json_str.contains("typescript"));
        
        // 测试反序列化
        let deserialized_config: TestLspManagerConfig = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized_config.servers.len(), config.servers.len());
        assert_eq!(deserialized_config.global_settings.enable_diagnostics, config.global_settings.enable_diagnostics);
    }

    #[test]
    fn test_custom_server_config() {
        let mut config = TestLspManagerConfig::default();
        
        // 添加自定义服务器配置
        let custom_server = TestLspServerConfig {
            name: "Custom Language Server".to_string(),
            command: "custom-lsp".to_string(),
            args: vec!["--custom-arg".to_string()],
            file_extensions: vec![".custom".to_string()],
            language_ids: vec!["custom".to_string()],
            enabled: true,
            auto_restart: false,
            max_restart_count: 5,
            restart_interval: 10,
            health_check_interval: 60,
        };
        
        config.servers.insert("custom".to_string(), custom_server);
        
        // 验证自定义配置
        let custom_config = config.servers.get("custom").unwrap();
        assert_eq!(custom_config.name, "Custom Language Server");
        assert_eq!(custom_config.command, "custom-lsp");
        assert_eq!(custom_config.args, vec!["--custom-arg".to_string()]);
        assert!(!custom_config.auto_restart);
        assert_eq!(custom_config.max_restart_count, 5);
        
        // 测试通过扩展名查找
        let found_server = config.get_server_for_extension(".custom");
        assert!(found_server.is_some());
        assert_eq!(found_server.unwrap().command, "custom-lsp");
    }

    #[test]
    fn test_global_settings_modification() {
        let mut config = TestLspManagerConfig::default();
        
        // 修改全局设置
        config.global_settings.enable_diagnostics = false;
        config.global_settings.max_completion_items = 50;
        config.global_settings.diagnostic_delay_ms = 1000;
        config.global_settings.max_concurrent_servers = 5;
        
        // 验证修改
        assert!(!config.global_settings.enable_diagnostics);
        assert_eq!(config.global_settings.max_completion_items, 50);
        assert_eq!(config.global_settings.diagnostic_delay_ms, 1000);
        assert_eq!(config.global_settings.max_concurrent_servers, 5);
    }
}
